package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class WelcomeServlet
 */
public class WelcomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public WelcomeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession(true);

		if (session.isNew()) {
			RequestDispatcher dispatcher = getServletConfig()
					.getServletContext().getRequestDispatcher("/login.html");
			dispatcher.forward(request, response);
		} else {
			String userName = (String) session.getAttribute("userName");

			out.println("<HTML><HEAD><TITLE> Welcome     " + userName
					+ "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n"
					+ "<H1 ALIGN=\"CENTER\">   Welcome    " + userName + "</H1>\n"
					+ "<H2>Information on Your Session:</H2>\n"
					+ "<TABLE BORDER=1 ALIGN=CENTER>\n"
					+ "<TR BGCOLOR=\"#FFAD00\">\n"
					+ "  <TH>Info Type<TH>Value\n" + "<TR>\n" + "  <TD>ID\n"
					+ "  <TD>" + session.getId() + "\n" + "<TR>\n"
					+ "  <TD>Creation Time\n" + "  <TD>"
					+ new Date(session.getCreationTime()) + "\n" + "<TR>\n"
					+ "  <TD>Time of Last Access\n" + "  <TD>"
					+ new Date(session.getLastAccessedTime())
					+ "\n   </BODY></HTML>");
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

}
